import 'package:amplitude_flutter/amplitude.dart';
import 'package:dio/dio.dart';
import 'package:xvideos/tools_base/debug_log.dart';

import '../config/config.dart';
import '../tools_base/global_store/store.dart';
import '../tools_base/net/net_manager.dart';

enum GraphicsType { vip, coin }

///数据埋点 管理类
// 泡芙: 1
// 色中色: 7
// 蜜桃: 15
// 黑料: 13
// 妖精: 14
// 红杏: 16
// 无忧: 17
// 五月天: 18
// 瑶池: 19
// 知音: 20
// 春水: 21
const int appId = 31;

class EventTrackingManager {
  static EventTrackingManager? _instance;

  // Create the instance
  static Amplitude analytics = Amplitude.getInstance(instanceName: "short_video");

  factory EventTrackingManager() {
    _instance ??= EventTrackingManager._();
    return _instance!;
  }

  EventTrackingManager._();
  final Dio _dio = createDio();
  //视频埋点列表 videoId : true
  Map videos = {};

  static Map getUserInfo() {
    return {'会员': globalStore.isVIP() ? "是" : "否"};
  }

  static uploadEvent(String eventType, {Map<String, dynamic>? eventProperties}) {
    analytics.logEvent(eventType, eventProperties: eventProperties);
    analytics.uploadEvents();
  }

  ///类型 视频：1
  addVideoDatas(videoId, videoTitle) {
    if ((videos[videoId] ?? false)) {
      return;
    }
    videos[videoId] = true;
    final data = {
      'dataType': 1,
      'videoDatas': {'appId': appId, 'videoId': videoId, 'videoTitle': videoTitle, 'userType': globalStore.isVIP() ? 1 : 2}
    };
    _post(data, '视频');
  }

  ///类型 标签：2
  addTagsDatas(tagsName) {
    final data = {
      'dataType': 2,
      'tagDatas': {'appId': appId, 'tagsName': tagsName, 'userType': globalStore.isVIP() ? 1 : 2}
    };
    _post(data, '标签');
  }

  ///类型 充值：3
  //graphicsId  视频传视频id   横幅传banner   用户自己传user
  //graphicsType 充值类型 1 金币 2 VIP
  static String _graphicsId = 'user';
  static String _graphicsTitle = 'user';
  static GraphicsType _graphicsType = GraphicsType.coin;
  addVideoVipGraphicss(videoId, videoTitle, GraphicsType graphicsType) {
    _graphicsId = videoId;
    _graphicsTitle = videoTitle;
    _graphicsType = graphicsType;
    // _addVipGraphicss(videoId, videoTitle, graphicsType);
  }

  addBannerVipGraphicss(GraphicsType graphicsType) {
    _graphicsId = 'banner';
    _graphicsTitle = 'banner';
    _graphicsType = graphicsType;
    // _addVipGraphicss('banner', 'banner', graphicsType);
  }

  addUserVipGraphicss(GraphicsType graphicsType) {
    _graphicsId = 'user';
    _graphicsTitle = 'user';
    _graphicsType = graphicsType;
    // _addVipGraphicss('user', 'user', graphicsType);
  }

  addVipGraphicss() {
    final data = {
      'dataType': 3,
      'VipGraphicss': {
        'appId': appId,
        'graphicsId': _graphicsId,
        'graphicsTitle': _graphicsTitle,
        'graphicsType': _graphicsType == GraphicsType.coin ? 1 : 2,
        'userType': globalStore.isVIP() ? 1 : 2
      }
    };
    _post(data, '$_graphicsTitle${_graphicsType == GraphicsType.coin ? '钻石' : 'VIP'}充值');
  }

  _post(data, dataType) async {
    if (Config.dataBuriedPoint?.isNotEmpty != true) {
      return;
    }
    try {
      await _dio.post(Config.dataBuriedPoint ?? "", data: data);
      debugLog('EventTracking', "$dataType埋点成功");
    } catch (e) {
      debugLog('EventTracking', "event tracking...error:$e");
    }
  }
}
